package com.southwind.util;

import com.southwind.vo.PayRecordVO;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class ExcelUtil {
    @Value("${upload.excel}")
    private static String excelPath;
    /**
     * 根据模板导出缴费记录信息
     * @param info 缴费记录信息
     * @param outputStream HttpServletResponse 的输出流
     */
    public static void ExpPayRecordInfo(List<PayRecordVO> info, OutputStream outputStream) {
        POIFSFileSystem fs = null;
        int headRow = 2; // 表头所在的行
        try {
            // 加载模板文件
            String srcfile = excelPath+"车辆缴费明细表.xls"; // 模板文件路径
            System.out.println("模板文件路径"+srcfile);
            FileInputStream fis = new FileInputStream(srcfile);
            fs = new POIFSFileSystem(fis);
            HSSFWorkbook wb1 = new HSSFWorkbook(fs);
            HSSFSheet sheet = wb1.getSheetAt(0);

            // 获取数据并填充到表格中
            HSSFCellStyle style = wb1.createCellStyle();
            style.setBorderLeft(BorderStyle.THIN);
            style.setBorderRight(BorderStyle.THIN);
            style.setBorderTop(BorderStyle.THIN);
            style.setBorderBottom(BorderStyle.THIN);

            for (int i = 0; i < info.size(); i++) {
                int col = 0;
                PayRecordVO p = info.get(i);
                HSSFRow row = sheet.createRow(i + headRow);

                // 填充每一列的数据
                HSSFCell cell = row.createCell(col++);
                cell.setCellStyle(style);
                cell.setCellValue(p.getPayRecordId());

                cell = row.createCell(col++);
                cell.setCellStyle(style);
                cell.setCellValue(p.getPropertyName());

                cell = row.createCell(col++);
                cell.setCellStyle(style);
                cell.setCellValue(p.getParkName());

                cell = row.createCell(col++);
                cell.setCellStyle(style);
                cell.setCellValue(p.getNumber());

                cell = row.createCell(col++);
                cell.setCellStyle(style);
                cell.setCellValue(p.getPayType());

                cell = row.createCell(col++);
                cell.setCellStyle(style);
                cell.setCellValue(p.getAmount());

                cell = row.createCell(col++);
                cell.setCellStyle(style);
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                cell.setCellValue(format.format(p.getCreateTime()));
            }

            // 将数据写入到浏览器输出流
            wb1.write(outputStream);
            wb1.close();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
